﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MainFrame
{
    public partial class PageProductQuery : Form
    {
        public PageProductQuery()
        {
            InitializeComponent();
        }
        private List<ProductInfo> lst_pros;
        private void btn_queryPro_Click(object sender, EventArgs e)
        {
            string querystr = "";
            string queryID = "";
            string queryResult = "";
    //      if (ckb_queryTime.Checked && !string.IsNullOrEmpty(dtp_q_timeStart.Text))
            {
                querystr += $"LaserTime >'{dtp_q_timeStart.Text}' and LabelTime <'{dtp_q_timeEnd.Text}'";
            }
            if (ckb_queryID.Checked && !string.IsNullOrEmpty(tbx_q_Id.Text))
            {
                if (string.IsNullOrEmpty(querystr))
                    querystr += $"ProID ='{tbx_q_Id.Text}'";
                else
                    querystr += $"and ProID ='{tbx_q_Id.Text}'";
            }
            if (ckb_queryResult.Checked && !string.IsNullOrEmpty(cmb_q_Product.Text))
            {
                if (string.IsNullOrEmpty(querystr))
                    querystr += $"typeName like '{cmb_q_Product.Text}%'";
                else
                    querystr += $"and typeName like '{cmb_q_Product.Text}%'";
            }
          
       
            if (!string.IsNullOrEmpty(querystr))
            {
                dgv_display.Rows.Clear();
                var lst = GlobalVar.sqlHelper.select<ProductInfo>(GlobalVar.userConfig.db_T_Product, querystr);
                if (lst != null)
                {
                    int Index = 0;
                    lst.ForEach(info =>
                    {
                        Index++;
                        dgv_display.Rows.Add(Index.ToString(),info.ProId, info.ProId_out, info.ProNo,
                            info.LaserTime == null ? "" : info.LaserTime?.ToString("yyyy-MM-dd HH:mm:ss"),
                            info.LabelTime == null ? "" : info.LabelTime?.ToString("yyyy-MM-dd HH:mm:ss"),
                            info.typeName, info.Result_up, info.Result_down, info.Result, info.Result_Cut, info.Result_Label);
                    });
                    lst_pros = lst;
                }

            }
        }
       
        private void btn_exportPro_Click(object sender, EventArgs e)
        {
            if (dgv_display.Rows.Count > 0 && lst_pros != null && lst_pros.Count > 0)
            {
                FolderBrowserDialog dirDialog = new FolderBrowserDialog();
                dirDialog.Description = "请选择导出文件夹";
                if (dirDialog.ShowDialog() == DialogResult.OK)
                {
                    var dirPath = dirDialog.SelectedPath;
                    WriteCSV(lst_pros, dirPath);
                }
            }
            else
            {
                MessageBox.Show("请先查询生产数据");
            }
        }
        public void WriteCSV(List<ProductInfo> infoList, string dirPath)
        {

            string saveDate = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss");
            var strPath = dirPath + "\\" + saveDate + ".CSV";
            FileStream fs = new FileStream(strPath, FileMode.CreateNew);
            StreamWriter csvStreamWriter = new StreamWriter(fs, Encoding.Default);
            string strSpile = ",";
            csvStreamWriter.WriteLine($"平台号{strSpile}工装ID{strSpile}出战RFID{strSpile}撕膜时间{strSpile}贴标时间{strSpile}生产配方{strSpile}上游结果{strSpile}下游结果{strSpile}结果{strSpile}撕膜结果{strSpile}贴标结果");

            infoList.ForEach(info => {
                var tstr1 = info.LabelTime == null ? " " : info?.LabelTime?.ToString("yyyy-MM-dd HH:mm:ss");
                var tstr2 = info.LaserTime == null ? " " : info?.LaserTime?.ToString("yyyy-MM-dd HH:mm:ss");
                var str = info.ProNo.ToString() + strSpile + info.ProId + strSpile + info.ProId_out + strSpile + tstr1 + strSpile + tstr2 + strSpile +
                  info.typeName + strSpile +
                  info.Result_up + strSpile +
                  info.Result_down + strSpile +
                  info.Result + strSpile + info.Result_Cut + strSpile + info.Result_Label;
                csvStreamWriter.WriteLine(str);
            });
            csvStreamWriter.Flush();
            csvStreamWriter.Close();
            fs.Close();
        }

        private void PageProductQuery_Load(object sender, EventArgs e)
        {
            if(GlobalVar.tv_list!=null&& GlobalVar.tv_list.Count>0)
            {
                cmb_q_Product.Items.AddRange(GlobalVar.tv_list.Select(a => a.type).Distinct().ToArray());
            }
        }
    }
}
